package com.runvp.netty.server.handler;


import com.runvp.common.config.RunvpConfig;
import com.runvp.common.enums.*;
import com.runvp.common.model.tcp.MessageReceiveBody;
import com.runvp.common.model.tcp.MessageResBody;
import com.runvp.common.utils.DateUtils;
import com.runvp.netty.build.BuildHttpClint;
import com.runvp.netty.constant.NettyConstant;
import com.runvp.service.biz.domain.BizDevice;
import com.runvp.service.build.BuildQRCode;
import com.runvp.service.tcp.TcpService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;


/**
 * 设备发送登录包 -设备自主发起
 */
@Slf4j
public class LoginHandle extends BizHandle{

    private MessageReceiveBody parseBody;

    private TcpService tcpService;

    public LoginHandle(TcpService tcpService, MessageReceiveBody parseBody){
        this.parseBody = parseBody;
        this.tcpService = tcpService;
    }
    /**
     * 设备发送登录包
     */
    public MessageResBody handle(Channel channel){
        long st = System.currentTimeMillis();
        try{
            //线程执行
//            LoginRunnable runnable = new LoginRunnable(channel,tcpService,parseBody);
////            ThreadPoolActuator.exeTask(runnable);
//            return runnable.handles();
            String type = parseBody.getTy();
            String sn = parseBody.getTerminalId();
            log.info("登录成功,参数ty:{},sn:{}执行通道绑定",type,sn);
            NettyConstant.addChannel(sn,channel);
            return BuildHttpClint.postJson("/app/cmd/login",parseBody);
        }finally {
            log.info("本次登录响应时间:【{} ms】",(System.currentTimeMillis()-st));
        }
    }


    /**
     * 初始化默认数据
     *
     * @return str
     */
    private void createBizDevice(MessageReceiveBody parseBody) {
        String sn = parseBody.getTerminalId();
        BizDevice query = new BizDevice();
        query.setDeviceCode(sn);
        List<BizDevice> deviceList = tcpService.instanceIBizDeviceService().selectBizDeviceList(query);
        BizDevice device;
        if (CollectionUtils.isEmpty(deviceList)) {
            device = new BizDevice();
            device.setDeviceType(DeviceType.BIG_SCREEN.getCode());
            device.setStatus(EnableStatus.DISABLE.getCode());
            device.setStockStatus(YesOrNo.YES.getCode());
            device.setDoorLockStatus(DoorLockStatus.CLOSE.getCode());
            device.setRenewStatus(RenewStatus.NORMAL.getCode());
            device.setDueDtime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, "2100-12-29 23:59:59"));
            device.setCreateTime(DateUtils.getNowDate());
            device.setDeviceCode(sn);
        } else {
            device = deviceList.get(0);
        }
        device.setLastUpdateTime(DateUtils.getNowDate());
        device.setLineStatus(LineStatus.ON_LINE.getCode());
        if (device.getId() == null) {
            tcpService.instanceIBizDeviceService().insertBizDevice(device);
            log.info("设备【{}】执行新增,执行时间:【{}】", sn, DateUtils.now());
        } else {
            device.setUpdateTime(DateUtils.getNowDate());
            device.setQrCode(BuildQRCode.createCode(device.getDeviceCode(), RunvpConfig.getPreview() + "/wechat.html?baseSn=" + device.getDeviceCode()));
            tcpService.instanceIBizDeviceService().updateBizDevice(device);
            log.info("设备【{}】执行修改,执行时间:【{}】", sn, DateUtils.now());
        }
    }
}
